package com.yh.modules.data.controller;

import com.yh.common.annotation.SysLog;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.R;
import com.yh.common.utils.ValidatorUtils;
import com.yh.modules.data.entity.ProjectEntity;
import com.yh.modules.data.service.ProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 项目
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("data:project:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = projectService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据部门id获取项目列表
     */
    @GetMapping("/list/{deptId}")
    @RequiresPermissions("data:project:list")
    public R getProjectListByDeptId(@PathVariable("deptId") Integer deptId) {
        List<ProjectEntity> projectEntityList = projectService.getProjectListByDeptId(deptId);
        return R.ok().put("projectList", projectEntityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("data:project:info")
    public R info(@PathVariable("id") Integer id) {
        ProjectEntity project = projectService.selectById(id);

        return R.ok().put("project", project);
    }

    /**
     * 保存
     */
    @SysLog(value = "新增项目")
    @RequestMapping("/save")
    @RequiresPermissions("data:project:save")
    public R save(@RequestBody ProjectEntity project) {

        ValidatorUtils.validateEntity(project);

        projectService.save(project);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog(value = "修改项目")
    @RequestMapping("/update")
    @RequiresPermissions("data:project:update")
    public R update(@RequestBody ProjectEntity project) {
        ValidatorUtils.validateEntity(project);

        projectService.update(project);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog(value = "删除项目")
    @RequestMapping("/delete")
    @RequiresPermissions("data:project:delete")
    public R delete(@RequestBody Integer[] ids) {
        projectService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
